package com.dongdong.service.impl;

import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.model.TbSpecificationExample;
import com.dongdong.model.TbSpecificationOption;
import com.dongdong.model.TbSpecificationOptionExample;
import com.dongdong.service.SpecificationOptionService;
import com.dongdong.vo.SpecificationOptionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecificationOptionServiceImpl implements SpecificationOptionService {

    @Autowired
    private TbSpecificationOptionMapper specificationOptionMapper;


    @Override
    public List<SpecificationOptionVO> findSpecificationOptionList(Long specId) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(specId);
        List<TbSpecificationOption> ops = specificationOptionMapper.selectByExample(example);
        // 转换
        List<SpecificationOptionVO> vos = ops.stream().map((x) -> {
            SpecificationOptionVO specOptionVO = new SpecificationOptionVO();
            BeanUtils.copyProperties(x, specOptionVO);
            return specOptionVO;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public void deletebySpecId(Long id) {
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> opsList = specificationOptionMapper.selectByExample(example);
        for (TbSpecificationOption tbSpecificationOption : opsList) {
            int i = specificationOptionMapper.deleteByPrimaryKey(tbSpecificationOption.getId());
        }

    }

    @Override
    public void deletebyId(Long id) {
        specificationOptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(TbSpecificationOption option) {
        specificationOptionMapper.insertSelective(option);
    }

    @Override
    public void addlist(List<TbSpecificationOption> collect, Long id) {
        for (TbSpecificationOption tbSpecificationOption : collect) {
            tbSpecificationOption.setSpecId(id);
            specificationOptionMapper.insert(tbSpecificationOption);
        }

    }

    @Override
    public void deleteAll(List<Long> list2) {
        for (Long l : list2) {
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            example.createCriteria().andSpecIdIn(Arrays.asList(l));
            specificationOptionMapper.deleteByExample(example);
        }
    }

    @Override
    public List<SpecificationOptionVO> findAllOption() {
        List<TbSpecificationOption> to = specificationOptionMapper.selectByExample(null);
        //转换
        List<SpecificationOptionVO>  vos = to.stream().map((x) -> {
            SpecificationOptionVO vo = new SpecificationOptionVO();
            BeanUtils. copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }


}
